using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Tasks;
using Nop.Core.Domain.Tasks;

namespace Nop.Plugin.Widgets.EKomi
{
    /// <summary>
    /// Live person provider
    /// </summary>
    public class EKomiPlugin : BasePlugin, IWidgetPlugin
    {
        private readonly ISettingService _settingService;
        private readonly IScheduleTaskService _scheduleTaskService;

        public EKomiPlugin(ISettingService settingService,
            IScheduleTaskService scheduleTaskService)
        {
            this._settingService = settingService;
            this._scheduleTaskService = scheduleTaskService;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return new List<string>() { "ekomi_widget" };
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "EKomi";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.EKomi.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "EKomi";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.EKomi.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }

        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {

            var settings = new EKomiSettings()
            {
                InterfaceId = "00000",
                InterfacePassword = "000000000000000",
                NoFeedbackForTheseSku = @"",

                WidgetScript = @"
<script type=""text/javascript"">
    var eKomiWGProt = ((""https:"" == document.location.protocol) ? ""https://"" : ""http://"");
    document.write(unescape(""%3Cscript src='"" + eKomiWGProt + ""connect.ekomi.de/widget/xyzxyzxyzxyzxyz.js' type='text/javascript'%3E%3C/script%3E""));
</script>",
                DisplayRating = false,
                DisplayStars = false,
                CommentsNumber = 100,

                SendMailToEKomi = false,
                Subject = "Order has been shipped from %Store.Name%",
                Body= @"<p>
Order has been shipped from %Store.Name%<br />
    Customer name %Order.CustomerFullName%<br />
    Order Number: %Order.OrderNumber%<br />
    Date Ordered: %Order.CreatedOn%<br />

Billing Address<br />
    %Order.BillingFirstName% %Order.BillingLastName%<br />
    %Order.BillingAddress1%<br />
    %Order.BillingCity% %Order.BillingZipPostalCode%<br />
    %Order.BillingStateProvince% %Order.BillingCountry%<br />

Shipping Address<br />
    %Order.ShippingFirstName% %Order.ShippingLastName%<br />
    %Order.ShippingAddress1%<br />
    %Order.ShippingCity% %Order.ShippingZipPostalCode%<br />
    %Order.ShippingStateProvince% %Order.ShippingCountry%<br />
    
Shipping Method: %Order.ShippingMethod%<br /> 

Shipped Products:<br />
    %Shipment.Product(s)%
</p>"

            };
            _settingService.SaveSetting(settings);

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Feedback", "Feedback");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.InterfaceId", "Interface id");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.InterfaceId.Hint", "Enter your interface id here (you can find it in the eKomi back office).");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.InterfacePassword", "Interface password");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.InterfacePassword.Hint", "Enter your interface password here (you can find it in the eKomi back office).");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.NoFeedbackForTheseSku", "List of SKU to be excluded from feedback");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.NoFeedbackForTheseSku.Hint", "List of SKU separated by ; if one of these SKU are found in the order we do not send ShippedEmail copy to eKomi");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.DisplayRating", "Display feedback rating");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.DisplayRating.Hint", "Check to display feedback rating");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.DisplayStars", "Display feedback stars");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.DisplayStars.Hint", "Check to display feedback stars");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.CommentsNumber", "Number of comments to display");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.CommentsNumber.Hint", "Enter the number of comments to display (random)");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.WidgetScript", "Widget script");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.WidgetScript.Hint", "Enter your widget script here (you can find it in the eKomi back office).");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.SendMailToEKomi", "Send email to ekomi");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.SendMailToEKomi.Hint", "Check if you want to send a mail to ekomi each time ShipmentSent.CustomerNotification is sended");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Email", "Mail to");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Email.Hint", "Mail to");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Subject", "Mail subject");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Subject.Hint", "Mail subject, with tokens");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Body", "Mail body");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Body.Hint", "Mail body, with tokens");


            //Install sync task
            InstallSyncTask();


            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //settings
            _settingService.DeleteSetting<EKomiSettings>();

            //locales
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.EKomi.Feedback", "Feedback");

            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.InterfaceId");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.InterfaceId.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.InterfacePassword");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.InterfacePassword.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.NoFeedbackForTheseSku");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.NoFeedbackForTheseSku.Hint");


            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.DisplayRating");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.DisplayRating.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.DisplayStars");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.DisplayStars.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.CommentsNumber");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.CommentsNumber.Hint");

            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.WidgetScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.WidgetScript.Hint");

            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.SendMailToEKomi");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.SendMailToEKomi.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Email");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Email.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Subject");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Subject.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Body");
            this.DeletePluginLocaleResource("Plugins.Widgets.EKomi.Body.Hint");


            //Remove scheduled task
            var task = FindScheduledTask();
            if (task != null)
                _scheduleTaskService.DeleteTask(task);


            base.Uninstall();
        }


        /// <summary>
        /// Installs the sync task.
        /// </summary>
        private void InstallSyncTask()
        {
            //Check the database for the task
            var task = FindScheduledTask();

            if (task == null)
            {
                task = new ScheduleTask
                {
                    Name = "Refresh EKomi Feedback",
                    //each 60 minutes
                    Seconds = 3600,
                    Type = "Nop.Plugin.Widgets.EKomi.RefreshFeedbackTask, Nop.Plugin.Widgets.EKomi",
                    Enabled = false,
                    StopOnError = false,
                };
                _scheduleTaskService.InsertTask(task);
            }
        }

        private ScheduleTask FindScheduledTask()
        {
            return _scheduleTaskService.GetTaskByType("Nop.Plugin.Widgets.EKomi.EKomiDesactivateGiftCardTask, Nop.Plugin.Widgets.EKomi");
        }
    }
}
